<template>
  <div class="login-container">
    <div class="box">
      <el-form ref="loginFormRef"
               class="login-form"
               :model="loginForm"
               :rules="loginFormRules"
               auto-complete="on"
               label-position="left">
        <div class="title-container">
          <h3 class="title">
            {{ $t('route.loginTitle') }}
          </h3>
          <Lang />
        </div>

        <el-form-item prop="mobile">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <!-- 用户名输入框 -->
          <el-input v-model.trim="loginForm.username"
                    placeholder="username"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="off" />
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input ref="password"
                    :key="passwordType"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="Password"
                    name="password"
                    tabindex="2"
                    auto-complete="on" />
          <span class="show-pwd"
                @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-button class="login-btn"
                   type="primary"
                   style="width: 100%; margin-bottom: 30px"
                   @click="handleLogin">{{ $t('route.login') }}</el-button>
        <!-- 底部提示 -->
        <div class="tips">
          <span style="margin-right:20px;">{{ $t('route.accountNumber') }}: root@admin.com</span>
          <span>{{ $t('route.password') }}: 123456</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
// 登录的实现流程：
// 1.创建组件陪路由
// 2.布局+美化(静态页面)
// 3.收集表单数据
// 4.校验
// 5.准备接口，提交登录
// 6.Vue和cookie共同管理token
export default {
  name: 'LoginView',
  data () {
    return {
      // 密码框类型
      passwordType: 'password',
      // 登录表单
      loginForm: {
        username: 'root@admin.com',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        // 手机号验证规则
        username: [
          { required: true, message: '请填写邮箱', trigger: 'blur' }
        ],
        // 密码验证规则
        password: [
          { required: true, message: '密码必填', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 切换密码框类型
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 处理登录
    handleLogin () {
      // 整体校验
      this.$refs.loginFormRef.validate(async (valid) => {
        if (valid) {
          try {
            // 1. 生成 shajs 实例
            const shajs = new this.$Shajs('SHA-256', 'TEXT', { encoding: 'UTF8' })
            // 2. 要加密的密码
            shajs.update(this.loginForm.password)
            // 3. 获取
            const pwd = shajs.getHash('HEX')
            console.log(pwd)
            // 发送请求
            await this.$store.dispatch('user/loginAction', {
              username: this.loginForm.username,
              password: pwd
            })
            this.$router.push('/')
          } catch (e) {
            //
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
// 样式变量
$bg: #283443;
$dark_gray: #889aa4;
$light_gray: #407ffe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $light_gray;
  }
}
.title-container {
  display: flex;
  justify-content: space-between;
}
// 登录容器
.login-container {
  background-image: url('../../assets/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  min-height: 100%;
  width: 100%;
  background-color: #f5f5f5;
  overflow: hidden;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 37px;
      caret-color: $light_gray;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 40px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    .login-btn {
      background: #407ffe;
      height: 64px;
      line-height: 32px;
      font-size: 24px;
    }
  }

  .tips {
    font-size: 14px;
    color: #000;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    .title {
      font-size: 26px;
      color: #000;
      margin: 0 0 30px;
      text-align: left;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.9); // 输入登录表单的背景色
    border-radius: 5px;
    color: $light_gray;
    .el-form-item__error {
      color: #fff;
      font-size: 14px;
    }
  }
}
.box {
  position: absolute;
  top: 30%;
  left: 15%;
  background-color: #f5f5f5;
  border-radius: 5px;
}
</style>
